﻿ using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

namespace SumThenMod
{
    /// <summary>
    /// SuperSerialPort
    /// </summary>
    public class SuperSerialPort
    {

        private SerialPort CommPort = new SerialPort();
        public bool isOpen { get; private set; }

        public SuperSerialPort()
        { 
            CommPort.DataReceived += serialport_DataReceived;
        }

        private StringBuilder SerialDataReceived = new StringBuilder();

        private void serialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(100);
            //读取数据
            int length = CommPort.BytesToRead;
            byte[] buffers = new byte[length];
            CommPort.Read(buffers, 0, length);
            //调用委托事件
            DataReceived.Invoke(buffers);
        }

        #region 公有属性
        /// <summary>
        /// 端口
        /// </summary>
        public string PortName
        {
            get { return CommPort.PortName; }
        }

        /// <summary>
        /// 获取 端口列表 
        /// </summary>
        public List<string> GetPortNames()
        {
            return SerialPort.GetPortNames().ToList<string>();
        }


        /// <summary>
        /// 获取 奇偶校验位 列表 
        /// </summary>
        /// <returns></returns>
        public List<string> GetParitys()
        {
            List<string> list = new List<string>();
            foreach (string str in Enum.GetNames(typeof(Parity)))
            {
                list.Add(str);
            }
            return list;
        }

        /// <summary>
        /// 获取 停止位 列表 
        /// </summary>
        /// <returns></returns>
        public List<string> GetStopBits()
        {
            List<string> list = new List<string>();
            foreach (string str in Enum.GetNames(typeof(StopBits)))
            {
                list.Add(str);
            }
            return list;
        }

        /// <summary>
        /// 打开串口
        /// </summary>
        public void Open()
        {
            if (!CommPort.IsOpen)
            {
                CommPort.Open();
            }
            isOpen = CommPort.IsOpen;
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void Close()
        {
            if (CommPort.IsOpen)
            {
                CommPort.Close();
            }
            isOpen = CommPort.IsOpen;
        }
        #endregion

        #region 设置串口信息
        /// <summary>
        /// 设置串口信息
        /// </summary>
        public void SetPortInfo(string PortName, string Parity, string Stop, string Data, string BaudRate)
        {
            CommPort.PortName = PortName;
            CommPort.Parity = (Parity)Enum.Parse(typeof(Parity), Parity);
            CommPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Stop);
            CommPort.DataBits = int.Parse(Data);
            CommPort.BaudRate = int.Parse(BaudRate);
        }
        #endregion 

        #region 接收事件
        /// <summary>
        /// 
        /// </summary>
        public Action<byte[]> DataReceived { get; set; }
        #endregion

        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="cmd">十六进制字符串</param>
        public void Send(string cmd)
        {
            if (!string.IsNullOrEmpty(cmd) && CommPort.IsOpen)
            {
                byte[] serOrder = Util.HexStringToByte(cmd);

                CommPort.Write(serOrder, 0, serOrder.Length);
            }
        }

        /// <summary>
        /// byte数组
        /// </summary>
        /// <param name="cmdbytes"></param>
        public void Send(byte[] cmdbytes)
        {
            MainWindow.Instance.updateSend(cmdbytes);
            if (cmdbytes?.Length > 0 && CommPort.IsOpen)
                CommPort.Write(cmdbytes, 0, cmdbytes.Length);
        }

    }
}
